package cn.getech.data.development.utils;

import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.BaseRes;
import cn.hutool.core.thread.ThreadUtil;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpDirEntry;
import sun.net.ftp.FtpProtocolException;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
public class FTPListAllFiles {
    private FtpClient ftpClient = null;

    /**
     * 登陆FTP服务器
     *
     * @param host     FTPServer IP地址
     * @param port     FTPServer 端口
     * @param username FTPServer 登陆用户名
     * @param password FTPServer 登陆密码
     * @return 是否登录成功
     * @throws IOException
     */
    public void login(String host, int port, String username, String password) throws Exception {
        try {
            SocketAddress addr = new InetSocketAddress(host, port);
            ftpClient = FtpClient.create();
            ftpClient.setConnectTimeout(5*1000);
            ftpClient.connect(addr);
            ftpClient.login(username, password.toCharArray());
        } catch (Exception e) {
            disConnection();
            throw e;
        }
    }

    /**
     * 关闭数据链接
     *
     * @throws IOException
     */
    public void disConnection() {
        if (null != ftpClient) {
            try {
                ftpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 递归遍历出目录下面所有文件
     *
     * @param pathName 需要遍历的目录，必须以"/"开始和结束
     * @throws IOException
     */
    public void  lists(List<String> arLLFiles,String pathName) throws Exception {
        log.error("ftp文件路径：{}",pathName);
        try {
            ftpClient.changeDirectory(pathName);
            Iterator<FtpDirEntry> ftpDirEntryIterator = ftpClient.listFiles(pathName);
            while(ftpDirEntryIterator.hasNext()){
                FtpDirEntry next = ftpDirEntryIterator.next();
                arLLFiles.add(next.getName());
            }
        } catch (Exception e) {
            log.error("file path is error:{}", e);
            disConnection();
            throw new RRException("file path is error!");
        }
    }


//    public boolean uploadFile(String path, String filename, InputStream input) {
//        boolean success = false;
//        try {
//            //更换目录到当前目录
//            ftp.changeWorkingDirectory(path);
//            ftp.storeFile(filename, input);
//            success = true;
//            input.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return success;
//    }


    public static void main(String[] args) throws Exception {
        FTPListAllFiles f = new FTPListAllFiles();
        List<String> tmpLists = new ArrayList<>();
//        //先登录
//        f.login("10.74.20.67", 21, "ftpadmin", "ftp_admin_123");
//        f.lists(tmpLists,"/test/");
//        f.disConnection();
//        System.out.println(tmpLists);




        //!1QAZ@2wsx
        //ftp://10.124.101.12
        //先登录
        f.login("10.124.101.12", 21, "obguser", "!1QAZ@2wsx");
        f.lists(tmpLists,"\\Export\\");
        f.disConnection();
        System.out.println(tmpLists);
    }
}
